import type { App } from 'vue';
import { createPinia } from 'pinia';
import type { Pinia } from 'pinia';
import { createPersistedState } from 'pinia-plugin-persistedstate';

let pinia: Pinia;
export async function initStores(app: App, options: { namespace: string }) {
  pinia = createPinia();
  const { namespace } = options;

  pinia.use(
    createPersistedState({
      key: (storeKey) => `${namespace}-${storeKey}`,
      storage: localStorage,
    }),
  );
  app.use(pinia);
  return pinia;
}

export function resetAllStores() {
  if (!pinia) {
    console.error('Pinia is not installed');
    return;
  }
  const allStores = (pinia as any)._s;
  for (const [_, store] of allStores) {
    store.$reset();
  }
}

export * from './modules';

export { defineStore, storeToRefs } from 'pinia';
